package com.hdu.hot100.stack;



public class lc394 {
    class Solution {
        int where;
        public String decodeString(String s)
        {
            return f(s.toCharArray(), 0);
        }


        String f(char[] s, int index)
        {
            int count = 0;
            StringBuilder ans = new StringBuilder();
            while (index < s.length && s[index] != ']')
            {
                if (Character.isDigit(s[index]))
                {
                    count = count * 10 + s[index++] - '0';
                }
                else if (s[index] == '[')
                {
                    String tmp = f(s, index + 1);
                    for (int i = 0; i < count; i++) {
                        ans.append(tmp);
                    }
                    count = 0;
                    index = where + 1;
                }
                else
                {
                    ans.append(s[index++]);
                }
            }
            where = index;
            return ans.toString();
        }
    }
}
